
package com.dlshouwen.wzgl.reload.controller;

import com.dlshouwen.core.base.http.HttpRequester;
import com.dlshouwen.core.base.model.AjaxResponse;
import com.dlshouwen.core.base.utils.AttributeUtils;
import com.dlshouwen.wzgl.baseinfo.dao.BaseInfoDao;
import com.dlshouwen.wzgl.baseinfo.model.BaseInfo;
import com.dlshouwen.wzgl.channel.dao.ChannelDao;
import com.dlshouwen.wzgl.channel.model.Channel;
import com.dlshouwen.wzgl.picture.dao.PictureDao;
import com.dlshouwen.wzgl.picture.model.Picture;

import freemarker.template.Configuration;
import freemarker.template.Template;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author cuixubin
 */
@Controller
@RequestMapping("wzgl/reloadCash")
public class ReloadCacheController {
	private PictureDao pictureDao;
	private BaseInfoDao baseInfoDao;
	private ChannelDao channelDao;

	@Resource(name = "baseInfoDao")
	public void setBaseInfoDao(BaseInfoDao baseInfoDao) {
		this.baseInfoDao = baseInfoDao;
	}
	
	@Resource(name = "pictureDao")
	public void setPictureDao(PictureDao pictureDao) {
		this.pictureDao = pictureDao;
	}

	@Resource(name = "ChannelDao")
	public void setChannelDao(ChannelDao channelDao) {
		this.channelDao = channelDao;
	}



	private HttpRequester requester;
    
    @Resource(name = "resquester")
    public void setRequester(HttpRequester requester) {
        this.requester = requester;
    }
    
    
    /**
     * 更新网站页面
     *
     * @param request 请求对象
      */
    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    @ResponseBody
    public AjaxResponse reFresh(HttpServletRequest request) {
    	String positon = AttributeUtils.getAttributeContent(request.getServletContext(), "static_html_location");
        AjaxResponse ajaxResp = new AjaxResponse();
        
        try {
        	createIndex(request, positon);
        	ajaxResp.setSuccess(true);
            ajaxResp.setSuccessMessage("更新成功！");
        }catch(Exception e) {
        	e.printStackTrace();
            ajaxResp.setError(true);
            ajaxResp.setErrorMessage("更新失败！网站缓存接口未配置。");
        }
        return ajaxResp;
    }
    
    /**
     * 生成首页
     * @param request
     * @param path
     * @return
     * @throws Exception 
     */
    private Boolean createIndex(HttpServletRequest request, String path) throws Exception {
    	Boolean ifOK = true;
    	Writer wout = null;
    	
    	//轮播图
    	List<Picture> lunboPicList = pictureDao.getPicturesByFlag("0");
    	//栏目一基本信息
    	Channel channelOne = channelDao.getChannelById("91dc5d77ad70c30795cb2813e6f9ee67");
    	//栏目二基本信息
    	Channel channelTwo = channelDao.getChannelById("866af936983c6cb48d73041600a15b00");
    	//栏目三基本信息
    	Channel channelThree = channelDao.getChannelById("b5f3d1976203a13382660e42af195b2c");
    	//副轮播图
    	List<Picture> tedianList = pictureDao.getPicturesByFlag("1");
    	//情景展示
    	List<Picture> qingjingList = pictureDao.getPicturesByFlag("2");
    	//施工展示
    	List<Picture> shigongList = pictureDao.getPicturesByFlag("3");
    	//基本信息
    	BaseInfo baseInfo = baseInfoDao.select();
    	//二维码
    	List<Picture> codePicList = pictureDao.getPicturesByFlag("4");
    	
		try {
			// 通过FreeMarker的Confuguration读取相应的模板文件
			Configuration configuration = new Configuration();
			configuration.setDefaultEncoding("UTF-8"); 
			configuration.setServletContextForTemplateLoading(request.getServletContext(), null);
			Template t = null;
            t = configuration.getTemplate("WEB-INF/ftl/index.ftl"); // 模板名
            t.setEncoding("UTF-8");
	        
	        path += "/index.html";//生成文件名
	        File outFile = new File(path);
	        
            wout = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(outFile),"UTF-8"));
	 
        	Map<String, Object> dataMap = new HashMap<String, Object>();
        	dataMap.put("lunboPicList", lunboPicList);
        	dataMap.put("channelOne", channelOne);
        	dataMap.put("channelTwo", channelTwo);
        	dataMap.put("channelThree", channelThree);
        	dataMap.put("tedianList", tedianList);
        	dataMap.put("qingjingList", qingjingList);
        	dataMap.put("shigongList", shigongList);
        	dataMap.put("baseInfo", baseInfo);
        	dataMap.put("codePicList", codePicList);
        	
        	dataMap.put("hello", "helloWorld");
            t.process(dataMap, wout);
		} catch (Exception e) {
			e.printStackTrace();
			ifOK = false;
		} finally { 
			if(wout != null) {
				try {
					wout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
    	
    	return ifOK;
    }
}
